<script lang="ts" setup>
import { useForm } from '@/hooks/web/useForm'
import { FormSchema } from '@/types/form'
import * as PosterApi from '@/api/system/poster'
import { ref, reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'

const message = useMessage()
const { t } = useI18n()

const dialogVisible = ref(false)
const dialogTitle = ref('新增金刚区')
const formLoading = ref(false)
const formType = ref<'create' | 'update'>('create')

const schema = reactive<FormSchema[]>([
  {
    field: 'posterUrl',
    label: '推广海报',
    value: '',
    component: 'UploadImg',
    formItemProps: {
      rules: [{ required: true, message: '请上传金刚区图标' }]
    }
  },
  {
    field: 'posterType',
    label: '推广海报类型',
    component: 'Select',
    value: 1,
    componentProps: {
      options: [
        { label: '报名海报', value: 0 },
        { label: '课程海报', value: 1 }
      ]
    },
    formItemProps: {
      rules: [{ required: true, message: '请输入推广海报类型' }]
    }
  },
  {
    field: 'ifSwitch',
    label: '是否开启跳转',
    value: false,
    component: 'Switch',
    componentProps: {
      activeValue: 1,
      inactiveValue: 0
    }
  },
  {
    field: 'qrCodeWidth',
    label: '二维码宽',
    component: 'InputNumber'
  },
  {
    field: 'qrCodeHeight',
    label: '二维码高',
    component: 'InputNumber'
  },
  //   {
  //     field: 'qrCodePositionTop',
  //     label: '二维码上边距',
  //     component: 'InputNumber'
  //   },
  {
    field: 'qrCodePositionRight',
    label: '二维码右边距',
    component: 'InputNumber'
  },
  {
    field: 'qrCodePositionBottom',
    label: '二维码下边距',
    component: 'InputNumber'
  }
  //   {
  //     field: 'qrCodePositionLeft',
  //     label: '二维码左边距',
  //     component: 'InputNumber'
  //   }
])

const { register, elFormRef, methods: formMethods } = useForm()

const open = (type: 'create' | 'update', id?: string) => {
  dialogVisible.value = true
  dialogTitle.value = type === 'update' ? '编辑Poster' : '新增Poster'
  formType.value = type

  if (type === 'update') {
    formLoading.value = true
    PosterApi.getDetailPoster(id).then((data) => {
      console.log('update: ', data)
      formMethods.setValues(data)
      formLoading.value = false
    })
  }
}
defineExpose({ open })

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!elFormRef.value) return
  const valid = await elFormRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true

  try {
    const formData = (await formMethods.getFormData()) || {}

    if (formType.value === 'create') {
      console.log('创建diy：', formData)
      await PosterApi.createPoster(formData)
      message.success(t('common.createSuccess'))
    } else {
      console.log('更新diy：', formData)
      await PosterApi.updatePoster(formData)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="700">
    <Form :schema="schema" @register="register" />
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
